package cn.com.pansky.action.bank;

import java.io.IOException;
import java.util.LinkedHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.vsame.url2sql.sql.Model;
import cn.com.pansky.payment.PayConf;
import cn.com.pansky.util.DateUtil;
import cn.com.pansky.util.PropertiesUtil;

import com.abc.pay.client.JSON;
import com.abc.pay.client.ebus.PaymentRequest;

/**
 * 农行支付
 * @author Administrator
 *
 */
public class ABCPayment extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ABCPayment() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、生成订单对象
		PaymentRequest tPaymentRequest = new PaymentRequest();
		tPaymentRequest.dicOrder.put("PayTypeID", "ImmediatePay"); //设定交易类型(ImmediatePay：直接支付,PreAuthPay：预授权支付,DividedPay：分期支付)
		tPaymentRequest.dicOrder.put("OrderDate", DateUtil.current10Time()); //设定订单日期 （必要信息 - YYYY/MM/DD）
		tPaymentRequest.dicOrder.put("OrderTime", DateUtil.current8Time()); //设定订单时间 （必要信息 - HH:MM:SS）
		tPaymentRequest.dicOrder.put("ExpiredDate", ""); //设定订单保存时间,非必项
		tPaymentRequest.dicOrder.put("CurrencyCode", PayConf.ORDERCURRENCY); //设定交易币种必要信息
		tPaymentRequest.dicOrder.put("OrderNo", request.getParameter("orderNumber")); //设定订单编号
		tPaymentRequest.dicOrder.put("OrderAmount", request.getParameter("orderAmount")); //设定交易金额request.getParameter("orderAmount")
		tPaymentRequest.dicOrder.put("Fee", ""); //设定手续费金额
		tPaymentRequest.dicOrder.put("OrderURL", ""); //设定订单地址
		tPaymentRequest.dicOrder.put("ReceiverAddress", ""); //收货地址
		tPaymentRequest.dicOrder.put("InstallmentMark", "0"); //分期标识(1:分期，0：不分期)
	    tPaymentRequest.dicOrder.put("InstallmentCode", ""); //设定分期代码
	    tPaymentRequest.dicOrder.put("InstallmentNum", ""); //设定分期期数
	    //充值类 0101:支付账户充值
	    //消费类 0201:虚拟类,0202:传统类,0203:实名类
	    //转账类 0301:本行转账,0302:他行转账
	    //缴费类 0401:水费,0402:电费,0403:煤气费,0404:有线电视费,0405:通讯费,
	    //0406:物业费,0407:保险费,0408:行政费用,0409:税费,0410:学费,0499:其他
	    //理财类 0501:基金,0502:理财产品,0599:其他
		tPaymentRequest.dicOrder.put("CommodityType", "0203"); //设置商品种类
		tPaymentRequest.dicOrder.put("BuyIP", ""); //客户IP
		tPaymentRequest.dicOrder.put("OrderDesc", ""); //订单说明
		tPaymentRequest.dicOrder.put("orderTimeoutDate", ""); //订单有效期

		//2、订单明细
		LinkedHashMap orderitem = new LinkedHashMap();
		orderitem.put("SubMerName", "");    //设定二级商户名称
		orderitem.put("SubMerId", "");    //设定二级商户代码
		orderitem.put("SubMerMCC", "");   //设定二级商户MCC码 
		orderitem.put("SubMerchantRemarks", "");   //二级商户备注项
		orderitem.put("ProductID", "");//商品代码，预留字段
		orderitem.put("ProductName", "城乡居民养老");//商品名称
		orderitem.put("UnitPrice", "");//商品总价
		orderitem.put("Qty", "1");//商品数量
		orderitem.put("ProductRemarks", ""); //商品备注项
		orderitem.put("ProductType", "");//商品类型
		orderitem.put("ProductDiscount", "");//商品折扣
		orderitem.put("ProductExpiredDate", "");//商品有效期
		tPaymentRequest.orderitems.put(1, orderitem);
		
		//3、生成支付请求对象
		// 1：农行卡支付
		// 2：国际卡支付
		// 3：农行贷记卡支付
		// 5：基于第三方的跨行支付
		// 6：银联跨行支付
		// 7：对公户
		// A:支付方式合并
		tPaymentRequest.dicRequest.put("PaymentType", "A"); //设定支付类型
		// 1：internet 网络接入
		// 2：手机网络接入
		// 3：数字电视网络接入
		// 4：智能客户端
		tPaymentRequest.dicRequest.put("PaymentLinkType", request.getParameter("payType")); //设定支付接入方式
	    tPaymentRequest.dicRequest.put("UnionPayLinkType", ""); //当支付类型为6，支付接入方式为2的条件满足时，需要设置银联跨行移动支付接入方式
		tPaymentRequest.dicRequest.put("ReceiveAccount", ""); //设定收款方账号
		tPaymentRequest.dicRequest.put("ReceiveAccName", ""); //设定收款方户名
		// 0：URL页面通知 
		// 1：服务器通知
		tPaymentRequest.dicRequest.put("NotifyType", "1"); //设定通知方式
		tPaymentRequest.dicRequest.put("ResultNotifyURL", PayConf.abcMercUrl); //设定通知URL地址
		Model model = (Model)request.getSession().getAttribute("user");
		tPaymentRequest.dicRequest.put("MerchantRemarks", model.getString("AA001")+"@"+request.getSession().getAttribute("orderWriteCA006").toString()); //设定附言
		tPaymentRequest.dicRequest.put("IsBreakAccount", "0"); //设定交易是否分账(0：否1：是)
		tPaymentRequest.dicRequest.put("SplitAccTemplate", ""); //分账模版编号        

		// 单商户支付
//		JSON json = tPaymentRequest.postRequest();
		// 多商户支付
		PropertiesUtil proUtil = new PropertiesUtil();
		// 根据商户号获取到所在属性文件中的索引
		int aae051Index = proUtil.getabcAAE051(request.getSession().getAttribute("AAE051").toString());
		JSON json = tPaymentRequest.extendPostRequest(aae051Index);

		String ReturnCode = json.GetKeyValue("ReturnCode");
		String ErrorMessage = json.GetKeyValue("ErrorMessage");
		if (ReturnCode.equals("0000")){
			System.out.println("支付请求===================="+"ReturnCode   = [" + ReturnCode + "]<br/>");
			System.out.println("支付请求===================="+"PaymentURL = "+json.GetKeyValue("PaymentURL"));
			response.sendRedirect(json.GetKeyValue("PaymentURL"));
		} else {
			System.out.println("支付请求===================="+"ReturnCode   = [" + ReturnCode + "]<br/>");
			System.out.println("支付请求===================="+"ErrorMessage = [" + ErrorMessage + "]<br/>");
		}
	}
}
